package utils

import "embed"

// ScanEmbedFsDo 扫描文件操作
// 注意：下划线文件不会被 embed.FS 嵌入打包进来
func ScanEmbedFsDo(embFs embed.FS, fn func(file string, path string) error) error {
	return scanFsDo(embFs, ".", fn)
}

func scanFsDo(embFs embed.FS, dir string, fn func(file string, path string) error) error {
	files, err := embFs.ReadDir(dir)
	if err != nil {
		return err
	}
	for _, f := range files {
		//文件路径
		path := dir + "/" + f.Name()
		if dir == "." {
			path = f.Name()
		}

		//目录则继续扫描
		if f.IsDir() {
			if err := scanFsDo(embFs, path, fn); err != nil {
				return err
			}
			continue
		}
		//文件则func处理
		if err := fn(f.Name(), path); err != nil {
			return err
		}
	}
	return nil
}
